package org.elephas.model;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.elephas.persistence.Dao;
import org.springframework.transaction.annotation.Transactional;

import salve.depend.Dependency;

/**
 * <p>
 * User: Nick Heudecker
 * </p>
 * <p>
 * Date: Jan 27, 2008
 * </p>
 * <p>
 * Time: 6:02:34 PM
 * </p>
 */
@Entity
public class Post extends AbstractPersistent {

	@ManyToOne(optional = false)
	private Blog blog;

	@Column(length = 500, nullable = false)
	private String title;

	@ManyToMany
	@JoinTable(name = "Post_Categories", joinColumns = @JoinColumn(name = "Post_uuid"), inverseJoinColumns = @JoinColumn(name = "Category_uuid"))
	private Set<Category> categories = new HashSet<Category>();

	@Column(length = 5000)
	private String content;

	@Column(length = 255)
	private String excerpt;

	@OneToMany(mappedBy = "post")
	private List<Comment> comments;

	@Enumerated(EnumType.STRING)
	private CommentStatus commentStatus;

	@Enumerated(EnumType.STRING)
	private PostStatus postStatus;

	@ManyToOne(optional = false)
	private User user;

	@Dependency
	private Dao dao;

	public void addComment(Comment comment) {
		getComments().add(comment);
		comment.setPost(this);
	}
	
	@Transactional
	public void saveComment(Comment comment) {
		dao.saveOrUpdate(comment);
	}

	@Transactional
	public void deleteCommend(Comment comment) {
		dao.delete(comment);
	}

	public Blog getBlog() {
		return blog;
	}

	public void setBlog(Blog blog) {
		this.blog = blog;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Set<Category> getCategories() {
		return categories;
	}

	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getExcerpt() {
		return excerpt;
	}

	public void setExcerpt(String excerpt) {
		this.excerpt = excerpt;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public CommentStatus getCommentStatus() {
		return commentStatus;
	}

	public void setCommentStatus(CommentStatus commentStatus) {
		this.commentStatus = commentStatus;
	}

	public PostStatus getPostStatus() {
		return postStatus;
	}

	public void setPostStatus(PostStatus postStatus) {
		this.postStatus = postStatus;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
